//
//  RJOutVC.m
//  AirQuality
//
//  Created by Po on 15/4/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJOutVC.h"
#import "RJOutView.h"
#import "RJBarView.h"
#import "RJWarmView.h"
#import "RJOutCityView.h"
#import "RJOutLineView.h"
#import "RJOutModel.h"
#import "RJOutDetailVC.h"
#import "RJOutPhotoVC.h"
#import "RJOutIntroduction.h"
#import "HistogramView.h"
#import "outBottomView.h"
#import "StationDetailController.h"
#import <objc/runtime.h>
#import <objc/message.h>

@interface RJOutVC () < RJOutCityViewDelegate,
                        UIScrollViewDelegate,
                        RJMainSuperDelegate,
                        RJOutViewDelegate,
                        outBottomViewDelegate>


@property (retain, nonatomic) RJOutView * outView;                  //View
@property (retain, nonatomic) UIView * citysControlView;            //城市列表手势背景层
@property (retain, nonatomic) UIButton * currentSectionButton;
@property (retain, nonatomic) RJOutCityView * outCityView;
@property (retain, nonatomic) RJBarView * myBarView;
@property (retain, nonatomic) RJWarmView * warmView;
@property (retain, nonatomic) RJOutModel * model;
@property (retain, nonatomic) RJOutLineView * lineBox;

@property (retain, nonatomic) RJLoadView * myloadView;
@property (strong, nonatomic) HistogramView * histogramView; //柱状图
@property (strong, nonatomic) outBottomView * bottomView; //下方列表

@property (assign, nonatomic) BOOL isPush;
@property (assign, nonatomic) BOOL isNetFail;                       //网络失败
@property (assign, nonatomic) NSUInteger currentCityCount;          //当前城市


@property (assign, nonatomic) NetRequestStatus aqi24NetRequest;     //24小时数据
@property (assign, nonatomic) NetRequestStatus realTimeRequest;     //实时数据
@property (assign, nonatomic) NetRequestStatus mapData;          //百度地图数据
@property (assign, nonatomic) BOOL  isCO;
@property (strong, nonatomic) NSMutableDictionary * city_pullutionDic;
@property (strong, nonatomic) NSMutableDictionary * city_AQIValueDic;

- (void)initInterface;
- (void)initNet;
@end

@implementation RJOutVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _isPush = NO;
    _isNetFail = NO;
    [self initInterface];
    [self initNet];
    objc_msgSend(self, @selector(noLoctionLoad));
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_isPush) {
        [self reSetAllUI];
    }
    if (!_currentSectionButton) {
        [_outView.sectionAQIButton setSelected:YES];
        [_outView.sectionAQIButton setBackgroundColor:GetRGBColor(59, 168, 252, 1)];
        _currentSectionButton = _outView.sectionAQIButton;
    }
    _paopaoName = [RJUserData defaultRJUserData].paoPaoName;
    if (_paopaoName.length >= 2) {
        [self touchPaoPao:_paopaoName];
    }else{
#warning 不要定位
//       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didGetLocationData:) name:@"didGetLocation" object:nil];
    }

}

#warning 不要定位新增方法
- (void)noLoctionLoad
{
#warning 不要定位
    if (![RJUserData defaultRJUserData].isProvince) {
        [_outView showMapView];
    }
    [_outView.positionLabel setText:@"无法定位"];
    [_outView.cityLabel setText:[RJUserData defaultRJUserData].paoPaoName];
    [_model updateWeatherCityName:[RJUserData defaultRJUserData].paoPaoName];
    NSArray * cityArr = @[@"成都",@"自贡",@"攀枝花",@"泸州"	,@"德阳",@"绵阳",@"广元",	@"遂宁",@"内江",@"乐山",@"南充",@"眉山"	,@"宜宾",@"广安",@"达州",@"雅安",@"巴中",@"资阳",@"马尔康",@"康定",@"西昌"];
    NSArray * codeArr = @[@"5101",@"5103",@"5104",@"5105",@"5106",@"5107",@"5108",@"5109",@"5110",@"5111",@"5113",@"5114",@"5115",@"5116",@"5117",@"5118",@"5119",@"5120",@"5132",@"5133",@"5134"];
    NSString * city = [RJUserData defaultRJUserData].paoPaoName;
     NSInteger index = [cityArr indexOfObject:city];
    NSString * code = codeArr[index];
    NSInteger  codeNum = [code integerValue];
    if (codeNum) {
        [_model setCurrentCityCode:@(codeNum)];
        [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",@(codeNum)]]; 
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"selectPaoPao" object:nil];
    _isPush = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    [_outView removeMap];
}

- (void)initNet
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"updateView" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isGetCity24Data:) name:@"updateLine" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateWeatherData:) name:@"updateWeather" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OutpositionValueIsGet:) name:@"OutPositionValueIsGet" object:nil];
    _aqi24NetRequest = NetRequestStatusDoing;
    _realTimeRequest = NetRequestStatusDoing;
    _model = [[RJOutModel alloc] init];
}

- (void)initInterface
{
    _outView = [[RJOutView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [_outView setDelegate:self];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_outView];
    [self.navBar setHidden:YES];
    [self initScrollView];
    _outView.AirRankLabel.layer.cornerRadius=10;
    [_outView.AirRankLabel.layer setMasksToBounds:YES];
    //    [_outView.barView addSubview:[self getBarView]];
    [self setDelegate:self];
    
    [_outView.lineView addSubview:[self getLineBoxWithData:nil]];
    [_outView.lineLabel.layer setCornerRadius:5];
    [self.view addSubview:[self getLoadView]];
}

- (void)initScrollView
{
    [_outView.detailScrollView setDelegate:self];
     _outView.detailScrollView.contentInset = UIEdgeInsetsMake(0, 0,200+MapHeight, 0);
    [self getBottomView];
}

- (RJOutLineView *)getLineBoxWithData:(NSDictionary *)data
{
    if (!_lineBox) {
        _lineBox = [[RJOutLineView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 10, 200) data:data succes:YES isCO:_isCO];
    }
    return _lineBox;
}


- (RJBarView *)getBarView
{
    if (!_myBarView) {
        CGRect rect = CGRectMake(0, 0-10, ScreenWidth, 230);
        NSString *cityName;
        if ([RJUserData defaultRJUserData].paoPaoName.length >=2 ) {
            cityName = [RJUserData defaultRJUserData].paoPaoName;
        }else{
            cityName = _model.currentName;
        }
        _myBarView = [[RJBarView alloc] initWithFrame:rect data:_model.cityAQIData[cityName]];
    }
    return _myBarView;
}

- (RJOutCityView *)getOutCityView
{
    if (!_outCityView) {
        _outCityView = [[RJOutCityView alloc] initWithFrame:CGRectMake(ScreenWidth - 75, 11, 63, 30) isShowTitle:YES];
        [_outCityView setData:_model.citys];
        [_outCityView setDelegate:self];
    }
    return _outCityView;
}

- (UIView *)getCitysControlView
{
    if (!_citysControlView) {
        _citysControlView = [[UIView alloc] initWithFrame:ScreenBounds];
        [_citysControlView setBackgroundColor:[UIColor grayColor]];
        [_citysControlView setAlpha:0.3];
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickCitysControlView)];
        [tapGesture setNumberOfTouchesRequired:1];
        [_citysControlView addGestureRecognizer:tapGesture];
    }
    return _citysControlView;
}

- (void)getBottomView
{
    if (!_bottomView) {
        _bottomView = [[outBottomView alloc] initWithFrame:CGRectMake(0, 730+MapHeight+35, ScreenWidth, 400)];
//        [_bottomView setAirKey:@"aqi"];
//        [_bottomView setDelegate:self];
        _bottomView.backgroundColor = [UIColor redColor];
        [_bottomView.layer setShadowColor:[UIColor grayColor].CGColor];
        [_bottomView.layer setShadowOffset:CGSizeMake(0, 0.5)];
        [_bottomView.layer setShadowOpacity:0.5];
        _bottomView.delegate = self;
        [_outView.detailScrollView addSubview:_bottomView];
        [_outView.detailScrollView bringSubviewToFront:_bottomView];
    }
}

- (RJLoadView *)getLoadView
{
    if (!_myloadView) {
        _myloadView = [[RJLoadView alloc] initWithFrame:self.view.bounds];
    }
    return _myloadView;
}

- (void)removeLoadView
{
    if (_myloadView) {
        [_myloadView removeSelf];
        _myloadView = nil;
    }
}

///**更新直线图数据位指定的type*/
//- (void)updateLineWithType:(NSString *)type
//{
//    NSDictionary * dic = [_model get24DataWithType:type];
//    //检测数据的完整性
//    NSArray * typeData = [dic[@"data"] allValues][0];
//    if (typeData.count < 24) {
//        //        [RJTools showTitle:@"异常" msg:@"获取的数据异常，请检查数据是否正确！" cancelString:@"知道了" vc:self];
//        return;
//    }
//    [_lineBox removeFromSuperview];
//    _lineBox = nil;
//    [_outView.lineView addSubview:[self getLineBoxWithData:dic]];
//}

- (void)updateLineWithType:(NSString *)type
{
    NSDictionary * dic = [_model get24DataWithType:type];
    NSDictionary * yearDic = [_model get365dataWithType:type];
    //检测数据的完整性
    NSArray * typeData = [dic[@"data"] allValues][0];
//    if (typeData.count < 24) {
//        //        [RJTools showTitle:@"异常" msg:@"获取的数据异常，请检查数据是否正确！" cancelString:@"知道了" vc:self];
//        return;
//    }
//    [_lineBox removeFromSuperview];
//    _lineBox = nil;
//    [_outView.lineView addSubview:[self getLineBoxWithData:dic]];
    if (_histogramView) {
        [_histogramView removeFromSuperview];
        _histogramView = nil;
    }
    if (!_histogramView) {
        _histogramView = [[HistogramView alloc] initWithFrame:CGRectMake(0, 500, ScreenWidth, 200) withData:dic withDayArr:yearDic];
        [_outView.detailScrollView addSubview:_histogramView];
        [_outView.detailScrollView bringSubviewToFront:_outView.typeSection];
    }
}
/**检测是否完成所有的网络请求，并移除加载动画*/
- (void)checkNetRequest
{
    if ((_aqi24NetRequest != NetRequestStatusDoing) && (_realTimeRequest != NetRequestStatusDoing)) {
        if ((_aqi24NetRequest == NetRequestStatusFailure) || (_realTimeRequest == NetRequestStatusFailure)) {
            if (!_isNetFail) {
                _isNetFail = YES;
                [RJTools showTitle:@"网络异常" msg:@"网络异常，请点击重试！" cancelString:@"取消" confirmString:@"重试" confirmBlock:^{
                    [_model refresh];
                    _isNetFail = NO;
                } vc:self];
            }
        }
        [self removeLoadView];
    }
}

/**更新天气View*/
- (void)setWeatherWithData:(NSDictionary *)data
{
    NSString * tempString = [NSString stringWithFormat:@"%@ %@°",data[@"weather"],data[@"temp"]];
    
    [_outView.weatherLabel setText:tempString];
    [_outView.windLabel setText:data[@"WS"]];
}

#pragma mark - notifaction
/**定位结束*/
- (void)didGetLocationData:(NSNotification *)data
{
    NSDictionary * dic = data.userInfo;
    if (![RJUserData defaultRJUserData].isProvince) {
        [_outView showMapView];
    }
    if ([dic[@"result"] integerValue] == 1) {
        [_outView.positionLabel setText:dic[@"Location"]];
        //自动设置城市
        NSUInteger count = 0;
        NSString * cityName = (_model.currentName ? _model.currentName : dic[@"CityName"]);
        
        //查询城市编号
        for (NSDictionary * cityData in _model.citys) {
            if ([[[cityData allValues] lastObject] isEqualToString:cityName]) {
                //                [self updateView];
                [_outCityView simulationPressCityWithRow:count];
                //请求详细数据（用于绘制折线图）
                NSNumber * cityCode = [self getCityCode:count];
                if (cityCode) {
                    [_model update24HourWithCityCode:cityCode];
                    [_model getStation365DayAQIForApp:[NSString stringWithFormat:@"%@",cityCode]];
                   [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",cityCode]];
                }
                return;
            }
            count ++;
        }
    } else if (_model.currentName) {
        [_outView.positionLabel setText:@"无法定位"];
        NSUInteger count = 0;
        for (NSDictionary * cityData in _model.citys) {
            if ([[[cityData allValues] lastObject] isEqualToString:_model.currentName]) {
                //                [self updateView];
                [_outCityView simulationPressCityWithRow:count];
                
                //请求详细数据（用于绘制折线图）
                NSNumber * cityCode = [self getCityCode:count];
                if (cityCode) {
                  [_model update24HourWithCityCode:cityCode];
                  [_model getStation365DayAQIForApp:[NSString stringWithFormat:@"%@",cityCode]];
                  [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",cityCode]];
                }
                return;
            }
            count ++;
        }
    }
#warning 不要定位
//    [_outView.positionLabel setText:@"无法定位"];
//    [_outView.cityLabel setText:@"成都"];
//    [_model updateWeatherCityName:@"成都"];
//    [_model setCurrentCityCode:@(5101)];
//    [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",@(5101)]];
    [_outView.positionLabel setText:@"无法定位"];
    [_outView.cityLabel setText:[RJUserData defaultRJUserData].paoPaoName];
    [_model updateWeatherCityName:[RJUserData defaultRJUserData].paoPaoName];
    NSString * code = [RJUserData defaultRJUserData].currentCityCode;
    NSInteger  codeNum = [code integerValue];
    if (codeNum) {
      [_model setCurrentCityCode:@(codeNum)];
      [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",@(codeNum)]];
    }
}

/**城市详细数据获取完毕*/
- (void)isGetCity24Data:(NSNotification *)data
{
    if (data.userInfo[@"result"]) {
        [self updateLineWithType:@"AQI"];
        _aqi24NetRequest = NetRequestStatusSucceed;
    } else {
        _aqi24NetRequest = NetRequestStatusFailure;
    }
    
    [self checkNetRequest];
}

- (void)updateWeatherData:(NSNotification *)data
{
    if ([data.userInfo[@"result"] isEqualToString:@"1"]) {
        [self setWeatherWithData:data.userInfo[@"data"]];
    } else {
        [_outView.weatherLabel setText:@"获取天气失败"];
    }
}

- (void)updateView:(NSNotification *)data
{
    if([data.userInfo[@"result"] isEqualToString:@"1"]){
        [self updateView];
        _realTimeRequest = NetRequestStatusSucceed;
    } else {
        _realTimeRequest = NetRequestStatusFailure;
    }
    [self checkNetRequest];
}

- (void)updateView
{
    //    [_outView.detailScrollView setContentSize:CGSizeMake(300, ScreenHeight + 619)];
    
    if (!_outCityView) {
        [self reSetAllUI];
        [self.view addSubview:[self getOutCityView]];
    }
    [self checkNetRequest];
}

#pragma mark - event
- (void)clickCitysControlView {
    if (_citysControlView) {
        [[self getOutCityView] setViewFrame:NO];
        [_citysControlView removeFromSuperview];
    }
}

#pragma mark - function
- (void)reSetAllUI
{
    [_myBarView removeFromSuperview];
    _myBarView = nil;
     NSString * cityName;
    if ([RJUserData defaultRJUserData].paoPaoName.length >=2 ) {
        cityName = [RJUserData defaultRJUserData].paoPaoName;
    }else{
        cityName = _model.currentName;
    }
    //上部标题与图片刷新
    NSString * aqiNum = [_model.cityAQIData[cityName] objectForKey:@"AQI"];
    [_outView.AQINumLabel setText:aqiNum];
    NSUInteger aqiNumInt = [aqiNum integerValue];
    CGRect rect = _outView.frame;
    if (aqiNumInt > 99) {
        rect.origin.x = 100;
    } else if(aqiNumInt > 9){
        rect.origin.x = 70;
    } else {
        rect.origin.x = 40;
    }
    [_outView.AQIDetailView setFrame:rect];
    NSString * pollutionString , *surePollutionString;
    _city_pullutionDic = _model.city_pullutionDic;
    _city_AQIValueDic = _model.city_AQIValueDic;
    NSString * string = _city_pullutionDic[cityName];
    if ([string rangeOfString:@"PM10"].location != NSNotFound) {
        pollutionString = [string  stringByReplacingOccurrencesOfString:@"PM10" withString:@"PM₁₀"];
    }else if ([string rangeOfString:@"PM2.5"].location != NSNotFound){
        pollutionString = [string stringByReplacingOccurrencesOfString:@"PM2.5" withString:@"PM₂.₅" ];
    }else{
        pollutionString = string;
    }
    if ([pollutionString rangeOfString:@"PM2.5"].location != NSNotFound) {
        surePollutionString = [pollutionString stringByReplacingOccurrencesOfString:@"PM2.5"  withString:@"PM₂.₅"];
    }else{
        surePollutionString = pollutionString;
    }
    [_outView.mainPollutionLabel setText:[NSString stringWithFormat:@"主要污染物:%@",surePollutionString]];
    [self reSetImage:[[_model.cityAQIData[cityName] objectForKey:@"AQI"] integerValue]];
    //时间
    [_outView.timeLabel setText:[RJTools getTimeWithFormat:@"HH:mm EEEE"]];
    [_outView.timeLabel.layer setShadowOffset:CGSizeMake(0, -1)];
    [_outView.dateLabel setText:[RJTools getTimeWithFormat:@"yyyy年MM月dd日" time:[NSDate date]]];
    //所有控件刷新
    [_outView.barView addSubview:[self getBarView]];    
    //更新提示
    if (_warmView) {
        [_warmView removeFromSuperview];
        _warmView = nil;
    }
    _warmView = [[RJWarmView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100) text:@{@"pollutant":string,@"aqi":_city_AQIValueDic[cityName]}];
    [_outView.detailScrollView setContentSize:CGSizeMake(0, 750+MapHeight)];
    [_outView.warmViewXib addSubview:_warmView];
}

- (void)reSetImage:(NSUInteger)aqiNum
{
    NSArray * rankTitleArray = @[@"优",@"良",@"轻度",@"中度",@"重度",@"严重"];
    NSArray * pandaImageArray = @[GetRGBColor(29, 217, 7, 1),
                                  GetRGBColor(200, 200, 11, 1),
                                  GetRGBColor(253, 123, 49, 1),
                                  GetRGBColor(229, 67, 12, 1),
                                  GetRGBColor(197, 0, 89, 1),
                                  GetRGBColor(137, 0, 37, 1)
                                  ];
    //    NSArray * pandaFace = @[@"Excellent",@"Good",@"Mild",@"Mild",@"Bad",@"Serious"];
    
    NSUInteger rank = [self getAQIRank:aqiNum];
    [_outView.AirRankLabel setText:rankTitleArray[rank]];
    [_outView.AirRankLabel setBackgroundColor:pandaImageArray[rank]];
    //    [_outView.pandaFaceImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Air%@",pandaFace[rank]]]];
}

- (NSNumber *)getCityCode:(NSUInteger)count
{
    NSArray * citys = [_outCityView getCitys];
    return [[citys[count] allKeys] lastObject];
}

- (NSUInteger)getAQIRank:(NSUInteger)aqiNum
{
    NSUInteger rank = (aqiNum - 1) / 50;
    if (rank > 5) {
        rank = 5;
    }
    return rank;
}

#pragma mark - RJOutViewDelegate

- (void)changeMapAndBottomWithType:(NSString *)type
{
     [_bottomView changeTypeKey:type];
}

- (void)OutView:(RJOutView *)outView pressExplainButton:(UIButton *)button
{
    RJOutIntroduction * introVC = [[RJOutIntroduction alloc] init];
    [self.navigationController pushViewController:introVC animated:YES];
}

#pragma mark -- 刷新
- (void)OutView:(RJOutView *)outView pressUpdateButton:(UIButton *)button
{
    //    [_outView.detailScrollView setContentOffset:CGPointZero animated:YES];
    [_outView typeSectionUpdata];
    [_bottomView changeTypeKey:@"AQI"];
    [_model refresh];
    [self.view addSubview:[self getLoadView]];
    [self reSetAllUI];
}


//- (void)OutView:(RJOutView *)outView pressTypeButton:(UIButton *)button
//{
//    NSString * type = button.titleLabel.text;
//    if ([button.titleLabel.text isEqualToString: @"PM"]) {
//        type = @"PM2.5";
//    }else if ([button.titleLabel.text isEqualToString: @"PM₁₀"]){
//      type = @"PM10";
//    }
//    _isCO = [button.titleLabel.text isEqualToString: @"CO"];
//    [self updateLineWithType:type];
//}

- (void)OutView:(RJOutView *)outView pressTypeButton:(UIButton *)button
{
    NSString * type = button.titleLabel.text;
//    if ([button.titleLabel.text isEqualToString: @"PM"]) {
//        type = @"PM2.5";
//    }else if ([button.titleLabel.text isEqualToString: @"PM₁₀"]){
//        type = @"PM10";
//    }
//    _isCO = [button.titleLabel.text isEqualToString: @"CO"];
//    [_bottomView changeTypeKey:type];
    [self updateLineWithType:type];
}
#pragma mark - RJMainSuperDelegate
- (void)RJMainSuperVCFreeAll
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateView" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"didGetLocation" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateLine" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updateWeather" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OutPositionValueIsGet" object:nil];
    [_bottomView removeNotification];
    [_outView freeData];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_outCityView setViewFrame:NO];
}

#pragma mark - RJCityBarDelegate
- (void)didSelectedRow:(NSUInteger)rowNumber data:(NSDictionary *)data
{
    _currentCityCount = rowNumber;
    [_outView typeSectionUpdata];
    [_bottomView changeTypeKey:@"AQI"];
//    [RJUserData defaultRJUserData].paoPaoName = @"";
    [_outView.detailScrollView setContentOffset:CGPointZero animated:YES];
    _model.currentName = [[_model.citys[rowNumber] allValues] lastObject];
    _model.currentCode = [[_model.citys[rowNumber] allKeys] lastObject];
    if (_model.currentCode) {
      [_model getBottomDataWithCityCode:_model.currentCode];
    }
    _model.currentPollutant = _model.primaryPollutants[rowNumber];
//    NSLog(@"**%@",_model.currentPollutant);
    _model.currentAQIValue = _model.AQIValues[rowNumber];
    _aqi24NetRequest = NetRequestStatusDoing;
    //        _realTimeRequest = NetRequestStatusDoing;
    [_model update24HourWithCityCode:@([_model.currentCode integerValue])];
    [_model getStation365DayAQIForApp:_model.currentCode];
    [_model updateWeatherCityName:_model.currentName];
    //    [_model updateData];
    [_outView.cityLabel setText:[data allValues][0]];
    if ([RJUserData defaultRJUserData].paoPaoName.length >= 2) {
        _model.currentName = [RJUserData defaultRJUserData].paoPaoName;
        NSArray * cityArr = @[@"成都",@"自贡",@"攀枝花",@"泸州"	,@"德阳",@"绵阳",@"广元",	@"遂宁",@"内江",@"乐山",@"南充",@"眉山"	,@"宜宾",@"广安",@"达州",@"雅安",@"巴中",@"资阳",@"马尔康",@"康定",@"西昌"];
        NSArray * codeArr = @[@"5101",@"5103",@"5104",@"5105",@"5106",@"5107",@"5108",@"5109",@"5110",@"5111",@"5113",@"5114",@"5115",@"5116",@"5117",@"5118",@"5119",@"5120",@"5132",@"5133",@"5134"];
        NSInteger index = [cityArr indexOfObject:_model.currentName];
        NSString * cityCode = codeArr[index];
        _model.currentCode = cityCode;
    }else{
      [self reSetAllUI];
    }
    [_model refresh];
    [self.view addSubview:[self getLoadView]];
    [_outView mapViewChangedWithCityName:_model.currentName cityCode:_model.currentCode];
//    [RJUserData defaultRJUserData].paoPaoName = _model.currentName;
}

#pragma mark - mapNotification
- (void)touchPaoPao:(NSString *)notification
{
    notification = [RJUserData defaultRJUserData].paoPaoName;
    NSArray * cityArr = @[@"成都",@"自贡",@"攀枝花",@"泸州"	,@"德阳",@"绵阳",@"广元",	@"遂宁",@"内江",@"乐山",@"南充",@"眉山"	,@"宜宾",@"广安",@"达州",@"雅安",@"巴中",@"资阳",@"马尔康",@"康定",@"西昌"];
    NSArray * codeArr = @[@"5101",@"5103",@"5104",@"5105",@"5106",@"5107",@"5108",@"5109",@"5110",@"5111",@"5113",@"5114",@"5115",@"5116",@"5117",@"5118",@"5119",@"5120",@"5132",@"5133",@"5134"];
    NSInteger index = [cityArr indexOfObject:notification];
    NSString * city = cityArr[index];
    NSString * code = codeArr[index];
    NSInteger intCode = [code integerValue];
    NSNumber * cityCode = [NSNumber numberWithInteger:intCode];
    if (cityCode) {
        [_model update24HourWithCityCode:cityCode];
        [_model getStation365DayAQIForApp:[NSString stringWithFormat:@"%@",cityCode]];
         [_model getBottomDataWithCityCode:[NSString stringWithFormat:@"%@",cityCode]];
    }
    [self didSelectedRow:index data:@{code:city}];
    
}
- (void)didPressCityBar:(BOOL)isShow
{
    if (isShow) {
        [self.view insertSubview:[self getCitysControlView] belowSubview:self.outCityView];
    } else if(_citysControlView) {
        [_citysControlView removeFromSuperview];
    }
}

- (void)OutpositionValueIsGet:(NSNotification *)data
{
    NSMutableArray * allStation = [NSMutableArray array];
//    NSMutableArray * displayRowArray = [NSMutableArray array];
    NSDictionary * dic = data.object;
    if (!dic) {
        return;
    }
    for (NSDictionary * columns  in dic) {
        NSDictionary * comlumn = columns[@"columns"];
        [allStation addObject:comlumn[@"STATIONNAME"]];
//        [displayRowArray addObject:[columns allValues]];
    }
    NSInteger lenght;
    NSInteger count = allStation.count+1;
     lenght = 30*count;
    [_outView.detailScrollView setContentInset:UIEdgeInsetsMake(0, 0,lenght, 0)];
}

#pragma mark -- 下方列表选择跳转
- (void)skipNextDetailPage:(NSDictionary *)dic
{
    StationDetailController * detailVC = [[StationDetailController alloc] init];
    detailVC.topDic = dic;
    [RJUserData defaultRJUserData].isYearData = NO;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - system
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
@end